package org.imlinker.login;

import java.util.ArrayList;
 
import org.imlinker.gui.LoginWindow;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

/**
 * 登录线程.
 */
public class Login extends Thread {

	public Login(String path) {
		this.path = path;

		// 初始化登录界面。
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				loginWindow = new LoginWindow(Login.this);
				loginWindow.setVisible(true);
			}
		});
	}

	public ArrayList<Account> getAccountList() {
		accounts = new Accounts(path);

		return accounts.getAccountList();
	}

	public XMPPConnection getConnection() { 
		return connection;
	}

	public void run() {

		boolean loginSuccess = false;
		final java.util.ResourceBundle bundle = java.util.ResourceBundle
				.getBundle("org/imlinker/gui/Bundle"); // NOI18N

		// 大循环，直到登录成功跳出。
		while (!loginSuccess) {

			// 等待设置account，
			final int sleepSeconds = 100;
			while (fullAccount == null) {
				try {
					Thread.sleep(sleepSeconds);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			// 开始连接服务器
			// 提示信息
			java.awt.EventQueue.invokeLater(new Runnable() {
				public void run() {
					loginWindow.setStatus(bundle.getString("Login.conntect"));
				}
			});

			String host = fullAccount.getHost();
			int port = Integer.parseInt(fullAccount.getPort());
			String domain = fullAccount.getDomain();
			ConnectionConfiguration config = new ConnectionConfiguration(host,
					port, domain);

			connection = new XMPPConnection(config);
			try {
				connection.connect();
			} catch (XMPPException e) {
				e.printStackTrace();
			}

			// 开始登录
			if (connection.isConnected()) {
				// 连接服务器成功，开始登录
				java.awt.EventQueue.invokeLater(new Runnable() {
					public void run() {
						loginWindow.setStatus(bundle
								.getString("Login.logining"));
					}
				});

				String username = fullAccount.getUser();
				String password = fullAccount.getPassword();

				try {
					connection.login(username, password);
				} catch (XMPPException e) {
				}

				if (connection.isAuthenticated()) {
					// 帐号登录成功

					loginSuccess = true;

					java.awt.EventQueue.invokeLater(new Runnable() {
						public void run() {
							loginWindow.setStatus(bundle
									.getString("Login.initialing"));
						}
					});
				} else {

					// 帐号登录失败
					java.awt.EventQueue.invokeLater(new Runnable() {
						public void run() {
							loginWindow.loginFailed(bundle
									.getString("Login.loginfailed"));
						}
					});

					fullAccount = null;
					saved = false;
				}

			} else {
				// 连接服务器失败的情况

				fullAccount = null;
				saved = false;

				java.awt.EventQueue.invokeLater(new Runnable() {
					public void run() {
						loginWindow.loginFailed(bundle
								.getString("Login.connectfailed"));
					}
				});
			}

		}

		// 登录成功后的步骤。
		// 保存帐号
		accounts.saveAccount(fullAccount, saved);

		loginWindow.setVisible(false);
		loginWindow.dispose();
	}

	public void setAccount(final Account account) {
		fullAccount = accounts.fillAccount(account);
	}

	public void setSaved(final boolean b) {
		saved = b;
	}

	private Account fullAccount;

	private LoginWindow loginWindow;

	private Accounts accounts;

	private boolean saved = false;

	private XMPPConnection connection;

	private String path;

}
